package com.health.controller;

import com.healthcommon.constant.MessageConstant;
import com.healthcommon.entity.Result;
import com.healthcommon.utils.RandomValidateCode;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@RestController
@RequestMapping("/login")
public class LoginController {

    //验证码输出
    @RequestMapping("/checkCode.do")
    public void checkCode(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        // 设置相应类型,告诉浏览器输出的内容为图片
        response.setContentType("image/jpeg");
        // 设置响应头信息，告诉浏览器不要缓存此内容
        response.setHeader("pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expire", 0);
        RandomValidateCode randomValidateCode = new RandomValidateCode();
        try {
            randomValidateCode.getRandcode(request, response);// 输出图片方法
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/getCode.do")
    public Result getCode(@RequestParam("checkcode") String checkcode, HttpServletRequest request)
            throws ServletException, IOException {

        String sessionCode = (String) request.getSession().getAttribute(RandomValidateCode.RANDOMCODEKEY);
        if (checkcode != null && checkcode.equalsIgnoreCase(sessionCode)) {
            return new Result(true, MessageConstant.LOGIN_SUCCESS,"1");
        }
        return new Result(false, MessageConstant.VALIDATECODE_ERROR,"0");
    }

}
